package com.yingside.gui.employee;

import com.lovo.netCRM.component.LovoButton;
import com.lovo.netCRM.component.LovoTable;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class EmployeeMainFrame extends JFrame {
    private LovoTable table;
    private LovoButton btnAdd,btnDel,btnEdit;
    public EmployeeMainFrame(){
        this.setTitle("员工管理窗口");
        this.setBounds(300, 300, 400, 300);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLayout(null);
        //初始化表格
        initTable();
        //初始化按钮
        initButton();
        //初始化表格数据
        initData();
    }

    /**
     * 初始化table
     */
    public void initTable(){
        table = new LovoTable(this,
                new String[]{"编号","姓名","性别","部门","电话"},
                new String[]{"id","name","sex","dept","tel"},
                "id");
        table.setSizeAndLocation(0,0,400,200);
    }

    /**
     * 初始化按钮
     */
    public void initButton(){
        btnAdd = new LovoButton("新增",50,230,this);
        btnDel = new LovoButton("删除",150,230,this);
        btnEdit = new LovoButton("修改",250,230,this);

        //新增操作
        btnAdd.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // todo 新增操作
            }
        });

        //删除操作
        btnDel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                int key = table.getKey();
                //是否选中table中的一行
                if(key == -1){
                    JOptionPane.showMessageDialog(null,"请先选中一行再进行删除！");
                    return;
                }
                //是否点击了确定
                int n = JOptionPane.showConfirmDialog(null,"你确定要删除吗?","删除确认",JOptionPane.OK_CANCEL_OPTION);
                if (n == 0){
                    // todo 删除操作
                }
            }
        });

        //修改操作
        btnEdit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int key = table.getKey();
                if(key == -1){
                    JOptionPane.showMessageDialog(null,"请先选中一行再进行操作");
                    return;
                }
                // todo 修改操作
            }
        });
    }

    /**
     * 给table赋值
     */
    public void initData(){
        // todo 需要给集合赋值
        table.updateLovoTable(null);
    }

    public static void main(String[] args) {
        EmployeeMainFrame employeeMainFrame = new EmployeeMainFrame();
        employeeMainFrame.setVisible(true);
    }
}
